<?php
namespace app\app\controller;

use think\Controller;
use think\Db;
use think\Session;

class Index extends Controller
{
    public function index()
    {
        $data = Db::field('name, address, isbreaker, wifi, parking, price, icon, g.id, information')->name('goods_img i')->join('goods g', 'g.id=i.gid')->where('face', '1')->select();
        // dump($data);die;
        return view('index/index',['data'=>$data]);
    }


    public function xq()
    {
        $id = input('get.id');
        $data = Db::name('goods')->where('id', $id)->find();
        $img = Db::field('icon')->name('goods_img')->where('gid', $id)->select();
        $goods = Db::name('room')->where('gid', $id)->paginate(10);
        // dump($goods);
        // dump($img);
        // dump($data);die;
        return view('xq/index',['data'=>$data, 'img'=>$img, 'goods'=>$goods]);
    }


    public function xd()
    {
        if (!Session::get('vip') == null) {
            $id = input('get.id');
            // dump($id);die;
            $data = Db::name('room')->where('id', $id)->find();
            // dump($data);die;
            return view('order/index',['data'=>$data]);
        }
        return view('login/login');
        exit;
    }


    public function order()
    {
        $id = Session::get('vip.id');
        $data = input('post.');
        $data['orderNum'] = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        $data['uid'] = $id;
        // dump($data);die;
        $res = Db::name('order')->insert($data);
        if (!$data['name'] == null) {
            if (!$data['tel'] == null) {
                $res = Db::name('order')->insert($data);
                if ($res > 0) {
                    $this->success('下单成功', 'app/index/index');
                } else {
                    $this->error('下单失败');
                }
                // JSON 返回
                return json($info);
            }else{
                $this->error('请填写联系号码');
            }
        }else{
            $this->error('请填写联系人');
        }
    }


    // 加载登录页
    public function login()
    {
        return view('login/login');
    }
    
    // 执行登录
    public function doLogin()
    {
        //接收用户名和密码
        $username = input('post.username');
        $userpass = input('post.userpass');

        //验证
        $data = Db::name('vip')->where('username', $username)->find();
        if (!$data) {
            $this->error('用户名不存在！');
            exit;
        }
        //验证密码
        if ($data['userpass'] != md5($userpass)) {
            $this->error('密码不正确');
            exit;
        }
        //把用户信息添加到session
        Session::set('vip', $data);
        
        $this->redirect('app/index/index');
        // return '111';
    }

    // 退出登录
    public function logout()
    {
        Session::delete('vip');
        $this->redirect('app/index/index');
    }


    // 加载注册页
    public function update()
    {
        return view('login/register');
    }

    // 执行注册
    public function doUpdate()
    {
        // 获取post数据
        $data = input('post.');
        // var_dump($data);die;
        // 判断data中uerpass和reuserpass是否相同
        if ($data['repass'] !== $data['userpass']) {
            $this->error('注册失败', 'app/login/register');
        }
        unset($data['repass']);
        $data['userpass'] = MD5($data['userpass']);
        // var_dump($data);die;  
        $query = Db::name('vip')->insert($data);
        // var_dump($query);die;
        $arr = array();
        $arr['uid'] = $query;
        $arr['rid'] = 7;
        // var_dump($arr);die;

        if ($query > 0) {
            $req = DB::name('user_role')->insert($arr);
        }

        if($query&&$req) {
            $this->success('注册成功', 'app/login/login');
        } else {
            Db::callback();
            $this->error('请稍后再试');
        }
    }
}
